package com.iflytek.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.entity.Classroom;
import com.iflytek.query.ClassroomQuery;
import com.iflytek.service.ClassroomService;
import com.iflytek.utils.ResultUtil;
import com.iflytek.vo.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("back/classroom")
public class ClassroomController {

    @Autowired
    private ClassroomService classroomService;


    @GetMapping("/list")
    public String list() {
        return "classroom/list";
    }

    @ResponseBody
    @GetMapping("/listData")
    public ResultData listData(ClassroomQuery query, Integer pageName, Integer pageSize) {
//        System.out.println(query);
        PageInfo<Classroom> pageInfo = classroomService.getAllClassroomList(query, pageName, pageSize);
        return ResultUtil.success(pageInfo.getTotal(), pageInfo.getList());
    }

    @GetMapping("/add")
    public String addUI() {
        return "classroom/add";
    }

    @ResponseBody
    @PostMapping("/add")
    public Map<String, Object> add(Classroom classroom) {
        int save = classroomService.saveClassroom(classroom);
        return ResultUtil.resultMap(save);
    }

    @GetMapping("/edit")
    public String editUI(Integer id, Model model) {
        model.addAttribute("classroom", classroomService.getById(id));
        return "classroom/edit";
    }

    @ResponseBody
    @PostMapping("/edit")
    public Map<String, Object> edit(Classroom classroom) {
        int update = classroomService.updateMsgById(classroom);
        return ResultUtil.resultMap(update);
    }

    @ResponseBody
    @PostMapping("/delete")
    public Map<String, Object> delete(Integer classroomId) {
        int delete = classroomService.deleteById(classroomId);
        return ResultUtil.resultMap(delete);
    }

    @ResponseBody
    @PostMapping("/batchDelete")
    public Map<String, Object> batchDelete(String ids) {
        int batchByDel = classroomService.delBatchByIds(Arrays.asList(ids.split(",")));
        return ResultUtil.resultMap(batchByDel);
    }




}
